/**
 * Created by DengYun on 2017/7/21.
 */
const Koa = require('koa');
const { StatusError } = require('koa-router-meta');
const router = require('./routers');

const app = new Koa();
app.title = "API文档 - 项目名称";
app.proxy = true;

app.use((ctx, next) => {
  return next()
    .then((info) => {
      ctx.body = ctx.body || {
          ok: 1,
          result: ctx.result,
        };
    }, err => {
      if (__DEV__) {
        console.error(err.stack);
      }
      const statusError = StatusError.from(err);
      ctx.status = statusError.status;
      ctx.body = statusError.toJson();
    });
});

app.use(router.routes());
app.use(() => {
  throw StatusError.NOT_FOUND('Invalid API Entry.');
});

module.exports = app;
